package com.nono.Dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.nono.bean.Comment;
import com.nono.bean.Commodity;

@Repository
public class CommentDao {
	@Autowired
	JdbcDao jdbcDao;
	
	/**
	 * @desc添加评论;
	 * */
	public boolean addComment(int userId, String userName, int commodityID, String comment) {
		
		return 0!=jdbcDao.jdbcTemplate.update("insert into comments value(null,?,?,?,?)", new Object[] {userId,userName, commodityID, comment});
		
	}
	/**
	 * @desc获取所有评论;
	 * */
	public List<Comment> getComments() {

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcDao.jdbcTemplate);
		Map<String, Object> paramMap = new HashMap<String, Object>();  
		RowMapper< Comment > rm = new BeanPropertyRowMapper<Comment>( Comment.class );
		List<Comment> comments = namedParameterJdbcTemplate.query("select * from comments",paramMap,rm);
		return comments;
	}
	
	/**
	 * 获取评论列表;
	 * */
	public List<Comment> getCommentById(int commodityId) {

		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcDao.jdbcTemplate);
		Map<String, Object> paramMap = new HashMap<String, Object>();  
		paramMap.put("commodityId", commodityId);
		RowMapper< Comment > rm = new BeanPropertyRowMapper<Comment>( Comment.class );
		List<Comment> comments = namedParameterJdbcTemplate.query("select * from comments where commodityId=(:commodityId)",paramMap,rm);
		return comments;
		
	}
}
